import "./login.css";
import { UserOutlined, KeyOutlined } from "@ant-design/icons";
import { connect } from "react-redux";
import { Button, Checkbox, Form, Input } from "antd";
import { useState, React } from "react";
import { getLogin } from "../../tools/Ajax";
import { useNavigate } from "react-router-dom";

function Login(props) {
  const navigate = useNavigate();
  const onFinish = (values) => {
    console.log(values);
    getLogin(values).then((res) => {
      console.log(7, res);
      if (res.data.code == 1) {
        navigate("/home")
        success:'登录成功'
      }
    });
  };
  const onFinishFailed = (errorInfo) => {
    console.log("Failed:", errorInfo);
  };
  return (
    <div className="login">
      <div className="box">
        <Form
          name="basic"
          labelCol={{
            span: 8,
          }}
          wrapperCol={{
            span: 16,
          }}
          style={{
            maxWidth: 600,
          }}
          initialValues={{
            remember: true,
          }}
          onFinish={onFinish}
          onFinishFailed={onFinishFailed}
          autoComplete="off"
        >
          <Form.Item
            className="ipt"
            // label="Username"
            name="userName"
            rules={[
              { required: true, message: "请输入用户名" },
              { max: 8, message: "用户名不超过8个字符" },
              {
                pattern: new RegExp(/^[\w\u4e00-\u9fa5]{2,8}$/, "g"),
                message: "用户名只能包含数字、字母、下划线、汉字2-8位",
              },
            ]}
          >
            <Input />
          </Form.Item>

          <Form.Item
            className="ipt"
            // label="Password"
            name="password"
            rules={[
              { required: true, message: "请输入密码" },
              { max: 12, message: "名称不超过12个字符" },
              {
                pattern: new RegExp(/^\w{6,12}$/, "g"),
                message: "密码只能包含数字、字母、下划线6-12位",
              },
            ]}
          >
            <Input.Password />
          </Form.Item>

          <Form.Item
            wrapperCol={{
              offset: 8,
              span: 16,
            }}
          >
            <Button type="primary" htmlType="submit">
              登录
            </Button>
          </Form.Item>
        </Form>
      </div>
    </div>
  );
}

function mapState(store) {
  return {
    count: store.count,
  };
}
Login = connect(mapState)(Login);

export default Login;
